<template>
	<view class="padding bg-white">
		<u-checkbox-group @change="selectChange">
			<u-checkbox v-model="item.checked" v-for="(item, index) in result.records" :key="index" :name="index">
				{{item.name}}
			</u-checkbox>
		</u-checkbox-group>
		<view class="margin-top">
			<u-button type="primary" @click="saveJoin">保存</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		onLoad: function(option) {
			let item = JSON.parse(option.item || '{}');
			if (item && item.id) {
				this.list(item);
			} else {
				this.$utils.modal("提示", "未选择角色", false, flag => {
					uni.navigateBack();
				});
			}
		},
		onShow: function() {},
		onHide: function() {},
		onReady: function() {},
		onReachBottom: function() {},
		data() {
			return {
				join: {
					type: null,
					srcId: null,
					srcCode: null,
					dstIds: null,
					dstCodes: null,
					dstList: null // ID前端列表
				},
				page: {
					current: 1,
					size: 200,
					total: 0,
					orders: [{
						column: "seq",
						asc: true
					}],
					cond: {
						code: null,
						name: null
					}
				},
				result: {
					current: 1,
					pages: 1,
					records: []
				}
			};
		},
		methods: {
			list(item) {
				this.$http.post("/admin/sys/rbac/perm/list", this.page, res => {
					let result = res.data || this.result;
					if (this.page.current > 1) {
						result.records = this.result.records.concat(result.records);
					}
					this.result = result;
					this.getJoin(item);
				}, false, false, true);
			},
			// 选择
			selectChange(val) {
				let dstList = [];
				if (val && val.length > 0) {
					for (let i in val) {
						dstList.push(this.result.records[val[i]]);
					}
				}
				this.join.dstList = dstList;
			},
			// 关联权限
			getJoin(row) {
				this.$http.post(
					"/admin/sys/rbac/join/getByTypeAndSrcId", {
						type: "role_perm",
						srcId: row.id
					},
					res => {
						this.join = {
							type: "role_perm",
							srcId: row.id,
							srcCode: row.code
						};
						let data = res.data;
						if (data) {
							this.join.dstIds = data.dstIds;
							this.join.dstCodes = data.dstCodes;
							let dstIdList = JSON.parse(this.join.dstIds || "[]");
							let dstCodeList = JSON.parse(this.join.dstCodes || "[]");
							if (dstIdList.length > 0 && dstCodeList.length > 0) {
								let dstList = [];
								let records = this.result.records || [];
								for (let i in dstIdList) {
									for (let j in records) {
										if (dstIdList[i] == records[j].id) {
											this.$set(records[j], "checked", true);
										}
									}
									dstList.push({
										id: dstIdList[i],
										code: dstCodeList[i]
									});
								}
								this.join.dstList = dstList;
							}
						}
					}
				);
			},
			saveJoin() {
				let idList = [];
				let codeList = [];
				if (this.join.dstList && this.join.dstList.length > 0) {
					for (let i in this.join.dstList) {
						if (this.join.dstList.hasOwnProperty(i)) {
							idList.push(this.join.dstList[i].id);
							codeList.push(this.join.dstList[i].code);
						}
					}
				}
				this.join.dstIds = JSON.stringify(idList);
				this.join.dstCodes = JSON.stringify(codeList);
				this.$http.post("/admin/sys/rbac/join/save", this.join, res => {}, true, true, true);
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>
